/**
 * 
 */
package com.jm.web.tags.table;

import com.jm.utils.StringHelper;
import com.jm.web.builder.table.TfootBuilder;
import com.jm.web.form.support.FormItem;
import com.jm.web.form.table.Tfoot;
import com.jm.web.tags.support.BaseTag;

/**
 * @author yanjie
 *
 * 2011/9/22 上午10:33:44
 */
public class TfootTag extends BaseTag{
	protected TableTag tableTag = null;
	protected Tfoot tfoot = null ;//表头有可能是一个table
	
	@Override
	public String getBuilderName() {
		return TfootBuilder.class.getName();
	}
	
	@Override
	public void preStartTag() {
		if(this.getParent() != null && this.getParent() instanceof TableTag){
			this.tableTag = (TableTag)this.getParent();
		}
		super.preStartTag();
	}
	
	/* 
	 * 不执行标签内部的内容
	 */
	@Override
	protected int onStartTag(){
		if(this.getPage().isExport() && !this.isEp()){
			return SKIP_BODY;
		}
		
		if(this.tableTag == null)
			return SKIP_BODY ;
		
		return EVAL_BODY_BUFFERED;
	}
	
	@Override
	protected void preEndTag() {
		super.preEndTag();
		//加载工具栏的配置
		String strXml = "";
		if(this.bodyContent != null)
			strXml = StringHelper.getValue(this.bodyContent.getString()).trim();
		
		tfoot = new Tfoot();
		tfoot.InitTag(this);
		tfoot.init();
		FormItem.LoadFromXML(StringHelper.Format("<tfoot>%1$s</tfoot>",strXml),tfoot);
		tfoot.init();
		tfoot.setCss("listing-tfoot");
	}
	
	@Override
	public int doEndTag() {
		if(this.getPage().isExportFile() && this.getPage().isFastExport() && this.tableTag.IsContainsKey("dataSet")){
			if(this.getBodyContent() != null){
				tfoot = new Tfoot();
				tfoot.InitTag(this);
				FormItem.LoadFromXML(StringHelper.Format("<tfoot>%1$s</tfoot>",this.bodyContent.getString().trim()),tfoot);
				tfoot.init();
				this.getPage().getExportAction().exportTfoot(this,tfoot);
			}
			return EVAL_PAGE;
		}else{
			return super.doEndTag();
		}
	}
	
	@Override
	public void clear() {
		tableTag = null ;
		tfoot = null;
		super.clear();
	}
	
	public Tfoot getTfoot() {
		return tfoot;
	}

	public void setTfoot(Tfoot tfoot) {
		this.tfoot = tfoot;
	}
	
	public TableTag getTableTag() {
		return tableTag;
	}
}
